package com.gmss.crawler.module.integrity.integritycase.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.module.integrity.integritycase.domain.IntegrityCase;
import com.gmss.crawler.module.integrity.integritycase.mapper.IntegrityCaseMapper;
import com.gmss.crawler.module.integrity.integritycase.service.IIntegrityCaseService;
import com.gmss.crawler.module.person.domain.PersonInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 科研诚信案件表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-11-25
 */
@Slf4j
@Service
public class IntegrityCaseServiceImpl extends ServiceImpl<IntegrityCaseMapper, IntegrityCase> implements IIntegrityCaseService {


    /**
     *
     * 获取以论文题目为单位的科研诚信案件信息
     *
     * @return
     * @throws IOException
     */
    public List<IntegrityCase> parseIntegrityCaseExcel(String filePath) throws IOException {
//        org.springframework.core.io.Resource resource = new ClassPathResource("/htmlTest/IntegrityCase-科研诚信案件/keyan科研诚信案件-11月.xlsx");
//        String filePath = resource.getFile().getAbsolutePath();
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(filePath)));
        //根据页面index 获取sheet页
        XSSFSheet sheet = wb.getSheetAt(1);        //实体类集合
        Row row = null;        //循环sesheet页中数据从第二行开始，第一行是标题
        //获取行数
        List<IntegrityCase> titleList = new ArrayList<>();
        int rows = sheet.getPhysicalNumberOfRows();

        XSSFRow firstRow = sheet.getRow(0);
        int firstCellNum = firstRow.getFirstCellNum();
        int lastCellNum = firstRow.getLastCellNum();
        for (int i = 1; i < rows; i++) {
            XSSFRow row1 = sheet.getRow(i);
            IntegrityCase integrityCase = new IntegrityCase();
            double v = Double.parseDouble(row1.getCell(0).toString());
            long excelId = new Double(v).longValue();
            integrityCase.setId(excelId);
            Date date = row1.getCell(1).getDateCellValue();
            integrityCase.setNotifyDate(date);
            integrityCase.setFirstAuthor(row1.getCell(2).getStringCellValue());
            integrityCase.setAndFirstAuthor(row1.getCell(3).getStringCellValue());
            integrityCase.setFirstReportAuthor(row1.getCell(4).getStringCellValue());
            integrityCase.setSecondAuthor(row1.getCell(5).getStringCellValue());
            integrityCase.setThirdAuthor(row1.getCell(6).getStringCellValue());
            integrityCase.setFourAuthor(row1.getCell(7).getStringCellValue());
            integrityCase.setFiveAuthor(row1.getCell(8).getStringCellValue());
            integrityCase.setSixAuthor(row1.getCell(9).getStringCellValue());
            integrityCase.setSevenAuthor(row1.getCell(10).getStringCellValue());
            integrityCase.setEightAuthor(row1.getCell(11).getStringCellValue());
            integrityCase.setOtherAuthor(row1.getCell(12).getStringCellValue());
            integrityCase.setTitle(row1.getCell(13).getStringCellValue());
            integrityCase.setNotifyLink(row1.getCell(14).getStringCellValue());
            integrityCase.setNotifySource(row1.getCell(15).getStringCellValue());
            integrityCase.setHandleResult(row1.getCell(16).getStringCellValue());
            titleList.add(integrityCase);
        }
        return titleList;
    }


    /**
     *
     * 获取以人名为单位的科研诚信案件信息
     *
     * @return
     * @throws IOException
     */
    public List<PersonInfo> parsePersonInfoIntegrityCaseExcel(String filePath) throws IOException {
//        org.springframework.core.io.Resource resource = new ClassPathResource("/htmlTest/IntegrityCase-科研诚信案件/keyan科研诚信案件-11月.xlsx");
//        String filePath = resource.getFile().getAbsolutePath();
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(filePath)));
        //根据页面index 获取sheet页
        XSSFSheet sheet = wb.getSheetAt(0);        //实体类集合
        Row row = null;        //循环sesheet页中数据从第二行开始，第一行是标题
        //获取行数
        List<PersonInfo> authorList = new ArrayList<>();
        int rows = sheet.getPhysicalNumberOfRows();
        for (int i = 1; i < rows; i++) {
            XSSFRow row1 = sheet.getRow(i);
            PersonInfo personInfo = new PersonInfo();
            double v = Double.parseDouble(row1.getCell(0).toString());
            long l = new Double(v).longValue();
            personInfo.setCaseId(l);
            personInfo.setName(row1.getCell(1).getStringCellValue());
            personInfo.setWorkCompany(row1.getCell(2).getStringCellValue().replaceAll("\\n", ""));
            if (row1.getCell(3) != null && StringUtils.isNotBlank(row1.getCell(3).getStringCellValue()))
                personInfo.setCompanyAddress(row1.getCell(3).getStringCellValue());
            if (row1.getCell(4) != null && StringUtils.isNotBlank(row1.getCell(4).getStringCellValue()))
                personInfo.setCompanyType(row1.getCell(4).getStringCellValue());
            if (row1.getCell(5) != null && StringUtils.isNotBlank(row1.getCell(5).getStringCellValue()))
                personInfo.setImgName(row1.getCell(5).getStringCellValue());
            if (row1.getCell(6) != null && StringUtils.isNotBlank(row1.getCell(6).getStringCellValue()))
                personInfo.setPersonTitle(row1.getCell(6).getStringCellValue());
            if (row1.getCell(7) != null && StringUtils.isNotBlank(row1.getCell(7).getStringCellValue()))
                personInfo.setAcademicDegree(row1.getCell(7).getStringCellValue());
            if (row1.getCell(8) != null && StringUtils.isNotBlank(row1.getCell(8).getStringCellValue()))
                personInfo.setNationality(row1.getCell(8).getStringCellValue());
            if (row1.getCell(9) != null && StringUtils.isNotBlank(row1.getCell(9).getStringCellValue()))
                personInfo.setNation(row1.getCell(9).getStringCellValue());
            if (row1.getCell(10) != null && StringUtils.isNotBlank(row1.getCell(10).getStringCellValue()))
                personInfo.setSex(row1.getCell(10).getStringCellValue());
            if (row1.getCell(11) != null && StringUtils.isNotBlank(row1.getCell(11).getStringCellValue()))
                personInfo.setSchool(row1.getCell(11).getStringCellValue());
            if (row1.getCell(12) != null && StringUtils.isNotBlank(row1.getCell(12).getStringCellValue()))
                personInfo.setMajor(row1.getCell(12).getStringCellValue());
            if (row1.getCell(13) != null && StringUtils.isNotBlank(row1.getCell(13).getStringCellValue()))
                personInfo.setDescribeData(row1.getCell(13).getStringCellValue());
            if (row1.getCell(14) != null && StringUtils.isNotBlank(row1.getCell(14).getStringCellValue()))
                personInfo.setSocialService(row1.getCell(14).getStringCellValue());
            if (row1.getCell(15) != null && StringUtils.isNotBlank(row1.getCell(15).getStringCellValue()))
                personInfo.setResearchContents(row1.getCell(15).getStringCellValue());
            if (row1.getCell(16) != null && StringUtils.isNotBlank(row1.getCell(16).getStringCellValue()))
                personInfo.setHonor(row1.getCell(16).getStringCellValue());
            personInfo.setAcademicianTitle(13);
            personInfo.setAcademicianTitleValue(AcademicianEnum.getEnum(13).getValue());
            authorList.add(personInfo);
        }
        return authorList;
    }

}
